import request from "@/api/request";


const MiniApp = window.MiniApp




function getUserId() {
  let data = JSON.parse(MiniApp.getStorageSync({key: "userInfo"}))
  let userId = data.openid
  return userId
}



const getUserInfo = () => {
  return new Promise((resolve, reject) => {
    MiniApp.getStorage({
      key: "userInfo",
      success(res) {
        let data = res.data
        if (data) {
          let userInfo = JSON.parse(data)
          resolve(userInfo)
        }
      },
      fail(err) {
        reject(err)
      }
    })
  })
}




const getRequest = (data, callback) => {
  return new Promise((resolve, reject) => {
    callback(data).then(res => {
      if (res.data) {
        resolve(JSON.parse(res.data))
      }
    }).catch(err => {
      reject(err)
    })
  })
}

const error = (err) => {
  MiniApp.showToast({
    title: err
  })
}



const uploadUserInfo = () => {
  MiniApp.getUserInfo({
    success(res) {
      console.debug("getUserInfo-res:"+JSON.stringify(res))
      let param = {}
      param.user_id = res.openid
      param.avatar = res.avatarUrl
      param.nick_name = res.nickName
      request.uploadUserInfo(param).catch(err => {
        error(err)
      })
      MiniApp.setStorage({
        key: "userInfo",
        data: res
      })
    },
    fail(err) {
      error(err.errMsg)
    }
  })
}


const exit=()=>{
  const userId=getUserId()
  if (userId){
    let data={
      user_id:userId
    }
    return request.exit(data)
  }else{
    error("获取用户Id失败")
  }
}

const globalConnection=()=>{
  const userId=getUserId()
  if (userId){
    request.globalConnection(userId).then()
  }
}


const getCurListen = (count = 1) => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
      count: count
    }
    return getRequest(data, request.getCurListen)
  } else {
    error("获取用户Id失败")
  }
}

const getMails = () => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
    }
    return getRequest(data, request.getMails)
  } else {
    error("获取用户Id失败")
  }
}

const addToLike = (music_id, music_name) => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
      music_id,
      music_name
    }
    return getRequest(data, request.collect)
  } else {
    error("获取用户Id失败")
  }
}

const getLikeList = () => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
    }
    return getRequest(data, request.getLikeList)
  } else {
    error("获取用户Id失败")
  }
}

const getFriends = () => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
    }
    return getRequest(data, request.getFriends)
  } else {
    error("获取用户Id失败")
  }
}

const getHistoryMusic = () => {
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
    }
    return getRequest(data, request.getHistoryMusic)
  } else {
    error("获取用户Id失败")
  }
}

const sendMail = (message, to_user_id) => {
  const theme = 'theme'
  const from_user_id = getUserId()
  if (from_user_id) {
    let data = {
      theme,
      from_user_id,
      to_user_id,
      msg: message
    }
    return getRequest(data, request.sendMail)
  } else {
    error("获取用户Id失败")
  }
}

const playMusic = (music_id, music_name) => {
  getUserInfo().then(userInfo=>{
    if (userInfo) {
      let data = {
        user_id: userInfo.openid,
        avatar: userInfo.avatarUrl,
        nick_name: userInfo.nickName,
        music_id,
        music_name
      }
      return getRequest(data, request.playMusic)
    } else {
      error("获取用户Id失败")
    }
  })
}

const getOutlineUser=()=>{
  const userId = getUserId()
  if (userId) {
    let data = {
      user_id: userId,
    }
    return getRequest(data, request.getOutlineMatch)
  } else {
    error("获取用户Id失败")
  }
}

const getRoomInfo =(roomId)=>{
  if (roomId) {
    let data = {
      room_id: roomId,
    }
    return getRequest(data, request.getRoomInfo)
  } else {
    error("roomId 不能為空")
  }
}

const cancelCollect=(music_id)=>{
  const userId=getUserId()
  if (userId){
    let data={
      user_id:userId,
      music_id
    }
    return getRequest(data,request.cancelCollect)
  }else{
    error("获取用户Id失败")
  }
}

const deleteHistory=(music_id)=>{
  const userId=getUserId()
  if (userId){
    let data={
      user_id:userId,
      music_id
    }
    return getRequest(data,request.deleteHistory)
  }else{
    error("获取用户Id失败")
  }
}

const deleteFriend=(his_user_id)=>{
  const userId=getUserId()
  if (userId){
    let data={
      my_user_id:userId,
      his_user_id
    }
    return getRequest(data,request.deleteHistory)
  }else{
    error("获取用户Id失败")
  }
}

const api = {
  uploadUserInfo,
  getCurListen,
  getMails,
  addToLike,
  getLikeList,
  getFriends,
  getHistoryMusic,
  sendMail,
  playMusic,
  exit,
  getOutlineUser,
  cancelCollect,
  deleteHistory,
  deleteFriend,
  globalConnection,
  getUserId,
  getRoomInfo
}
export default api;